package com.example.videoproject.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.videoproject.pojo.Remark;
import com.example.videoproject.pojo.Users;
import com.example.videoproject.pojo.Video;
import com.example.videoproject.pojo.Videolike;
import com.example.videoproject.service.IRemarkService;
import com.example.videoproject.service.IUsersService;
import com.example.videoproject.service.IVideoService;
import com.example.videoproject.service.IVideolikeService;
import com.example.videoproject.utils.Code;
import com.example.videoproject.utils.DateUtil;
import com.example.videoproject.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 二组
 * @since 2022-07-23
 */
@RestController
@RequestMapping("/video")
@Slf4j
public class VideoController {

    @Autowired
    private IVideoService videoService;

    @Autowired
    private IRemarkService remarkService;

    @Autowired
    private IVideolikeService videoLikeService;

    //分组查询并排序后
    @GetMapping("getAllVideo")
    public Result getAllVideo(){
        List<Video> list = videoService.list(null);

        Map<String, List<Video>> collect = list.stream()
                .sorted(Comparator.comparingInt(Video::getVcount).reversed())
                .collect(Collectors.groupingBy(Video::getVlabel));

        return new Result(Code.SELECT_OK,collect);
    }

    @GetMapping("innerVideo")
    public Result innerVideo(){
        Page<Video> page=new Page<>(1,3);
        QueryWrapper<Video> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("vcount");
        Page<Video> videoPage = videoService.page(page, wrapper);
        List<Video> videos = videoPage.getRecords();
        return new Result(Code.SELECT_OK,videos);
    }

    //条件查询带分页
    @PostMapping("/select")
    public  Result select( Integer current, Integer limit, String search,Integer flag){
        //创建page对象，传递当前页，每页记录数
        Page<Video> page = new Page(current,limit);
        QueryWrapper<Video> wrapper=new QueryWrapper<>();
        wrapper.like("vtitle",search.trim());
        if (flag==1){
            //按播放量排序
            wrapper.orderByDesc("vcount");
        }else if (flag==2){
            //按收藏量排序
            wrapper.orderByDesc("vcollect");
        }else if (flag==3){
            //按上传时间排序
            wrapper.orderByDesc("vuptime");
        }

        Page<Video> videoPage = videoService.page(page, wrapper);
        return new Result(Code.SELECT_OK,videoPage);
    }

    @GetMapping("getVideoById/{vid}")
    public Result getVideoById(@PathVariable Integer vid){
        Video video = videoService.getById(vid);
        QueryWrapper<Remark> wrapper=new QueryWrapper<>();
        wrapper.eq("parentuid",0).eq("vid",vid);
        List<Remark> list = remarkService.list(wrapper);
        return new Result(Code.SELECT_OK,list);
    }

    //视频点赞
    @PostMapping("Like")
    public Result videoLike(Integer vid,boolean videoLike){
        QueryWrapper<Video> wrapper=new QueryWrapper<>();
        wrapper.eq("vid",vid);
        Video one = videoService.getOne(wrapper);
        Integer vlike = one.getVlike();
        if (videoLike){
            vlike++;   //点赞就加1
        }else {
            --vlike;   //取消点赞
        }
        one.setVlike(vlike);
        boolean b = videoService.updateById(one);
        int code = b?Code.UPDATE_OK:Code.UPDATE_ERR;

        return new Result(code,one);
    }

    //视频收藏
    @PostMapping("collect")
    public Result videoCollect(Integer uid, Integer vid, boolean videoCollect){
        //先找到视频信息
        QueryWrapper<Video> wrapper=new QueryWrapper<>();
        wrapper.eq("vid",vid);
        Video one = videoService.getOne(wrapper);
        Integer vcollect = one.getVcollect();

        //查看收藏表是否收藏
        QueryWrapper<Videolike> wrapper1=new QueryWrapper<>();
        wrapper1.eq("vid",vid).eq("uid",uid);
        Videolike temp = videoLikeService.getOne(wrapper1);

        if (videoCollect && temp==null){
            vcollect++;   //收藏就加1
            //如果收藏中没有就添加
            //添加收藏表
            Videolike videolike = new Videolike(null,uid,vid,null);
            videoLikeService.save(videolike);
            one.setVcollect(vcollect);
            videoService.updateById(one);
            return new Result(1,null);  //收藏成功
        }else if(!videoCollect){
            --vcollect;   //取消收藏
            //从收藏表中删除
            QueryWrapper<Videolike> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("vid",vid);
            queryWrapper.eq("uid",uid);
            videoLikeService.remove(queryWrapper);

            one.setVcollect(vcollect);
            videoService.updateById(one);
            return new Result(0,null);  //取消收藏
        }
        //已收藏
        return new Result(2,null);
    }

    @PostMapping("getVideo")
    public Result getVideos(Integer vid){
        Video video = videoService.getById(vid);
        QueryWrapper<Video> wrapper=new QueryWrapper<>();
        wrapper.eq("vlabel",video.getVlabel());
        List<Video> list = videoService.list(wrapper);
        return new Result(Code.SELECT_OK,list);
    }

    @PostMapping("selectVideo")
    public Result selectVideo(Integer vid){
        Video video = videoService.getById(vid);
        return new Result(Code.SELECT_OK,video);
    }

    //查询所有视频信息
    @GetMapping("/listAllVideo")
    public List<Video> listVideo() {
        return videoService.list();
    }

    //按一定条件查询视频信息
    @RequestMapping("/listVideosBy")
    public List<Video> listVideosBy(Video video) {
        QueryWrapper<Video> qw = new QueryWrapper<Video>();
        if (video.getVid() != null) {
            qw.like("vid", video.getVid());
        }
        if (video.getVtitle() != null && video.getVtitle() != "' '") {
            qw.like("vtitle", video.getVtitle());
        }
        if (video.getVlabel() != null && video.getVlabel() != "' '") {
            qw.like("vlabel", video.getVlabel());
        }
        List<Video> list = videoService.list(qw);
        return list;

    }

    //删除视频信息
    @PostMapping("/deleteVideo")
    public boolean deldeteViedo(Integer vid) {
        return videoService.removeById(vid);
    }

    //更新视频信息
    @PostMapping("/updateVideo")
    public boolean updateVideo(Video video) {
        System.out.println(video);
        return videoService.updateById(video);
    }

    //添加视频信息
    @PostMapping("/insertVideo")
    public boolean insert(Video video) {
        video.setVuptime(DateUtil.getNow());//这是上传时间为当前系统时间
        video.setVcount(0);
        video.setVlike(0);
        video.setVcollect(0);
        video.setVdeleted(0);
        System.out.println(video);
        boolean msg = videoService.save(video);
        return msg;
    }


}

